package apptoko.com.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import apptoko.com.UILApplication;
import apptoko.com.model.CollectionList;
import apptoko.com.model.ContentItemInfo;

import com.androidquery.AQuery;
import apptoko.com.R;
;

public class CollectionListAdapter extends ArrayAdapter<CollectionList>{
	
	private LayoutInflater inflater;
	private AQuery aq;
	private View itemView;
	private AQuery aq1;
	private ContentItemInfo item;
	private LinearLayout parent;
	public CollectionListAdapter(Context context, int textViewResourceId, List<CollectionList> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		parent = new LinearLayout(context);
		parent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.collection_list_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.collection_name);
			holder.seeMore = (ImageButton) convertView.findViewById(R.id.btn_coll_seemore);
			holder.listItem = (HorizontalScrollView) convertView.findViewById(R.id.horiz_list_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		CollectionList collList = getItem(position);
		if(collList != null){
			aq = new AQuery(convertView);
			aq.id(holder.name).text(collList.getName());
			for(int i=0; i<collList.getContentList().size(); i++){
				item = collList.getContentList().get(i);
				itemView = inflater.inflate(R.layout.collection_item, null);
				aq1 = new AQuery(itemView);
				if(UILApplication.getInstance().isLoadAllImgs()){
					aq1.id(R.id.coll_icon).visibility(View.VISIBLE);
					if(aq1.getCachedFile(item.getAvatar())!=null){
						aq1.id(R.id.coll_icon).image(aq1.getCachedFile(item.getAvatar()),75);
					}else{
						Bitmap placeholder = aq.getCachedImage(R.drawable.image_loading);
						aq1.id(R.id.coll_icon).image(item.getAvatar(), false, true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK);
					}
					
				}else{
					aq1.id(R.id.coll_icon).visibility(View.GONE);
				}
				aq1.id(R.id.coll_name).text(item.getTitle());
				aq1.id(R.id.coll_price).text(R.string.free);
				aq1.id(R.id.coll_rating).rating(item.getRating());
				parent.addView(itemView);
			}
			holder.listItem.addView(parent);
		}
		return convertView;
	}
	
	private static class ViewHolder{
		TextView name;
		ImageButton seeMore;
		HorizontalScrollView listItem;
	}
}
